package com.qinyeit.serviceapp.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qinyeit.serviceapp.constans.Constants;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.service.branchs.BranchDataReleasedServiceImpl;
import com.qinyeit.serviceapp.service.branchstores.BranchPurchaseOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = Constants.QUEUE_MERCHANT_GROUP)
public class BranchCustomer {

    @Autowired
    private BranchPurchaseOrderService branchPurchaseOrderService;
    @Autowired
    private BranchDataReleasedServiceImpl branchDataReleasedService;

    @RabbitHandler
    public void msgHandler(String msg){
//        pur.saves();

        System.out.println(">>>>>>>>>>>>>rabit : ");
        System.out.println(JSONObject.toJSONString(msg));
        if (StringUtils.isBlank(msg)){
            return;
        }
        JSONObject data ;
        data = JSON.parseObject(msg);
        String type = data.getString("type");
        //单店 确认收货
        if ("complete".equals(type)){
//          data:{type:complete,order:123456,groupId:1}
            //通知总部 订单已确认收货
            //总部将订单修改为已完成
            branchPurchaseOrderService.updateOrderState(data.getLong("order"),data.getLong("groupId"));
        }else if("zicai".equals(type)){
//            data:{type:zicai,order:112,groupId:1}
            //单店自采 总部添加采购单
            branchPurchaseOrderService.saveOrderForZicai(data.getLong("order"),data.getLong("groupId"));
        }else if ("member".equals(type)){
            //会员操作(开卡/修改密码/修改资料)
            //data:{cardId:1,vipId:1,recordId:1,merchantId:1,groupId:1}
            branchDataReleasedService.openCard(data);
        }else if("memberUpdate".equals(type)){
            //修改会员信息
//            data:{vipId:1,merchantId:1,groupId:1}
            branchDataReleasedService.memberUpdate(data);
        }




    }
}
